# -*- coding: utf-8 -*-
import re
from syslog import *
from utils import threaded

try:
    import pygtk
    pygtk.require("2.0")
    import gtkhtml2
    import gnomevfs    
    import gtk
    import gtk.glade    
except:
    sys.stderr.write("ImportError: Module is not present %s" % cause)
    sys.exit(1)

COLOR1 = "#DDDDDD"
COLOR2 = "#FFFFFF"

# Create links for anchors    
def linkify(msg):
    reg = re.compile("http:\/\/(.*)\s?",re.I | re.M | re.DOTALL)
    return reg.sub(r'<a href="http://\1">http://\1</a>',msg)

# Bold the tags
def tagfy(msg):
    reg = re.compile("(#[a-zA-z]*\s?)",re.I | re.M | re.DOTALL)
    return reg.sub(r'<b>\1</b>',msg)

# Bol the ats
def atfy(msg):
    reg = re.compile("(@[a-zA-z]*\s?)",re.I | re.M | re.DOTALL)
    return reg.sub(r'<b>\1</b>',msg)

# Format the tags for twitter
def formatwi(msg):
    msg = linkify(msg)
    msg = tagfy(msg)
    msg = atfy(msg)
    return msg

class HTMLPanel():
    def __init__(self):
        # gtkHTML configurations
        self.document = gtkhtml2.Document()
        self.browser = gtkhtml2.View()
        self.browser.set_document(self.document)
        self.document.connect('request-url', self.request_image)

        # control the applyed colors
        self.msg_count = 0
        
        # Store html list
        self.html = ""
        
        # standard format for list
        self.html_format = u"""
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml" lang="pt" xml:lang="pt-br">
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />            
                <body style="margin: 0px 0px 0px 0px;padding: 0px 0px 0px 0px">
                %s
                </body>
            </html>
        """
            
        # message format
        self.msg_format = u"""
            <table style="background:%s;border:0px;" width="100%%" height="50">
                <tr>
                    <td>
                        <img src="%s" />
                    </td>            
                    <td >
                        <span style="text-align:left">%s</span>
                    </td>
                </tr>
            </table>                
        """
        
    @threaded
    def request_image(self,document,url,stream):
        try:
            f = gnomevfs.open (url, gnomevfs.OPEN_READ)
        except:
            syslog(LOG_ERR,"Gnomevfs error to open")
            stream.close ()
            return

        while 1:
            try:
                stream.write (f.read (1024))
            except gnomevfs.EOFError:
                syslog(LOG_ERR,"Gnomevfs error EOF")
                break

        f.close ()
        stream.close ()

    def widget(self):
        return self.browser

    # push html msg to list 
    def push(self,image,msg):
        color = [COLOR1,COLOR2][self.msg_count % 2]
        self.html += self.msg_format % (color,image,formatwi(msg))
        self.msg_count += 1

    # push list
    def push_list(self,msg_list):
        for img,msg in msg_list:
            self.push(img,msg)

    # write the html list to widget
    def write(self):
        self.document.open_stream("text/html")        
        self.document.write_stream(self.html_format % self.html)
        self.document.close_stream()

    def show(self):
        self.browser.show_all()

    def hide(self):
        self.browser.hide()
        

